package com.flowerSystem.controller;

import com.flowerSystem.entity.Address;
import com.flowerSystem.entity.User;
import com.flowerSystem.service.AddressService;
import com.flowerSystem.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@RequestMapping("/user")
@RestController
@CrossOrigin
public class AddressController {
    @Autowired
    AddressService addressService;
    //根据用户id查询用户地址
    @GetMapping("/address")
    public ResponseResult selectAllByUserId(int userId){
        return ResponseResult.ok(addressService.selectAllByUserId(userId));
    }

    /**
     * 根据用户id添加地址
     * @param address 用户数据
     * @return
     */
    @PostMapping("/address")
    public ResponseResult insertAll(@RequestBody Address address){
        boolean save = addressService.save(address);
        if (save){
            return ResponseResult.ok(address);
        }
        return ResponseResult.fail();
    }
    //根据用户id修改地址
    @PutMapping("/address")
    public ResponseResult update(@RequestBody Address address){
        boolean f =addressService.updateById(address);
        if (f){
            return ResponseResult.ok(address);
        }
        return ResponseResult.fail();
    }
    //根据id删除用户地址
    @DeleteMapping("/address")
    public ResponseResult delete(@RequestBody Address address){
        boolean f = addressService.removeById(address);
        if (f){
            return ResponseResult.ok(address);
        }
        return ResponseResult.fail();
    }

    //根据id查找地址
    @GetMapping("/user/addressById")
    public ResponseResult getAddressController(@RequestBody Address address){
        int id =address.getId();
        return ResponseResult.ok(addressService.getById(id));
    }
}
